package com.tju.ocean_ecology_website.controller;

import com.tju.ocean_ecology_website.dto.ApiResponse;
import com.tju.ocean_ecology_website.dto.PageResult;
import com.tju.ocean_ecology_website.entity.User;
import com.tju.ocean_ecology_website.entity.VolunteerComment;
import com.tju.ocean_ecology_website.entity.VolunteerInfo;
import com.tju.ocean_ecology_website.service.UserService;
import com.tju.ocean_ecology_website.service.VolunteerCommentService;
import com.tju.ocean_ecology_website.service.VolunteerInfoService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 志愿者评论控制器
 */
@RestController
@RequestMapping("/api/volunteer")
public class VolunteerCommentsController {

    @Resource
    private UserService userService;

    @Resource
    private VolunteerInfoService volunteerInfoService;

    @Resource
    private VolunteerCommentService volunteerCommentService;

    /**
     * 获取志愿者评论列表
     *
     * @param targetType 目标类型：0-活动, 1-项目, 2-新闻, 3-教育资源
     * @param targetId 目标ID
     * @param page 页码
     * @param pageSize 每页大小
     * @return 评论列表
     */
    @GetMapping("/comments")
    public ResponseEntity<ApiResponse<PageResult<VolunteerComment>>> getVolunteerComments(
            @RequestParam(value = "targetType", required = false) Integer targetType,
            @RequestParam(value = "targetId", required = false) Long targetId,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        // 创建查询条件
        VolunteerComment queryCondition = null;

        // 如果有targetType或targetId，则创建查询条件对象
        if (targetType != null || targetId != null) {
            queryCondition = new VolunteerComment();
            queryCondition.setStatus(0); // 正常状态

            if (targetType != null) {
                queryCondition.setTargetType(targetType);
            }

            if (targetId != null) {
                queryCondition.setTargetId(targetId);
            }
        }

        // 查询志愿者评论
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize);
        Page<VolunteerComment> commentPage = volunteerCommentService.queryByPage(queryCondition, pageRequest);

        // 创建分页结果
        PageResult<VolunteerComment> pageResult = PageResult.fromPage(commentPage, page);

        return ResponseEntity.ok(ApiResponse.success(pageResult));
    }

    /**
     * 删除评论
     *
     * @param id 评论ID
     * @return 删除结果
     */
    @DeleteMapping("/comments/{id}")
    public ResponseEntity<ApiResponse<Boolean>> deleteComment(@PathVariable("id") Long id) {
        // 查询评论信息
        VolunteerComment comment = volunteerCommentService.queryById(id);
        if (comment == null) {
            return ResponseEntity.badRequest().body(ApiResponse.error(400, "评论不存在"));
        }

        // 删除评论
        boolean result = volunteerCommentService.deleteById(id);

        return ResponseEntity.ok(ApiResponse.success(result, "删除评论成功"));
    }

    /**
     * 点赞/取消点赞评论
     *
     * @param id 评论ID
     * @return 点赞结果
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity<ApiResponse<Boolean>> toggleLike(@PathVariable("id") Long id) {
        // 查询评论信息
        VolunteerComment comment = volunteerCommentService.queryById(id);
        if (comment == null) {
            return ResponseEntity.badRequest().body(ApiResponse.error(400, "评论不存在"));
        }

        // 点赞/取消点赞
        boolean isLiked = volunteerCommentService.toggleLike(id, 1L);

        String message = isLiked ? "点赞成功" : "取消点赞成功";
        return ResponseEntity.ok(ApiResponse.success(isLiked, message));
    }


}
